<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Permission extends Model
{
    protected $guarded = [];

    public function children()
    {
        return $this->hasMany('App\Models\Permission','pid','id');
    }

    public function roles(){
        return $this->belongsToMany('App\Models\Role');
    }

    static function get_permissions($id){
        //生成分类缓存
        /* $categories = Cache::rememberForever('shop_category_catgories', function() {
             return self::with(['child'=>function($query){
                 $query->orderBy('sort_order');
             }])->where('parent_id',0)->orderBy('sort_order')->get();
         });
         return $categories;*/

        $permissions = self::with(['children'])->where('pid',0)->get();
        return $permissions;
    }
}
